<template>

  <div :class="{ isImgPdf_large: type === 'large', isImgPdf_small: type === 'small' }"
    :style="{ width: `${width}px`, height: `${height}px` }">
    <div v-if="isPdf(url) && type === 'large'" class="isImgPdf_pdf" @click="downloadPdf(url)">
      <img v-if="Number(width) >= 90" src="@/assets/home/pdf.png"
        :style="{ width: `${+width - 60}px`, height: `${+height - 60}px` }" alt="">
      <div v-if="Number(width) >= 90" class="product_image_div_pdf_buttton">预览</div>

      <img v-if="Number(width) < 90" src="@/assets/home/pdf.png" style="width: 100%;height: 100%;" alt="">
    </div>
    <img v-if="isPdf(url) && type === 'small'" src="@/assets/home/pdf.png" class="isImgPdf_pdf_img" alt=""
      @click="downloadPdf(url)">
    <van-image v-if="!isPdf(url)" :src="!url ? '' : url + '?x-oss-process=image/resize,w_300'" fit="cover"
      @click="enlargeImages(url)">
      <template v-slot:error>加载失败</template>
    </van-image>
  </div>
</template>

<script setup lang="ts">
import { defineProps } from 'vue'
import { showImagePreview } from "vant";
import { loginStore } from "@/store/login";
import { storeToRefs } from "pinia";
const store = loginStore();
const { token } = storeToRefs(store);
defineProps({
  url: { default: '' },
  width: { default: '90' },
  height: { default: '90' },
  type: { default: 'large' }
})
const isPdf = (url: string) => {
  return url.includes(".pdf")
}
const downloadPdf = (item: string) => {
  (window as any).uni.webView.postMessage({
    data: {
      type: 'download',
      url: item,
      header: {
        Authorization: `Bearer ${token.value}`,
        clientid: 'e5cd7e4891bf95d1d19206ce24a7b32e'
      }
    }
  })
}
/** 图片放大 */
const enlargeImages = (url: string) => {
  if (url) {
    showImagePreview({
      images: [url],
    });
  }
};
</script>

<style lang="less" scoped>
.isImgPdf_large {
  margin: 0 5px 5px 0;
}

.isImgPdf_small {
  margin-left: 5px;
}

.isImgPdf_pdf {
  width: 100%;
  height: 100%;
  border: 1px dashed #AFB6C3;
  display: flex;
  flex-flow: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;

  .product_image_div_pdf_title {
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: normal;
    font-size: 10px;
    margin-bottom: 5px;
  }

  .product_image_div_pdf_buttton {
    color: #0094FF;
  }
}

.isImgPdf_pdf_img {
  width: 100%;
  height: 100%;
}




:deep(.van-image) {
  width: 100%;
  height: 100%;
}
</style>